package com.authine.cloudpivot.ext.kingdee.jdbc;

import com.alibaba.cola.dto.RequestContext;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.KingDeeConstant;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.voucher.Voucher;
import com.authine.cloudpivot.ext.utils.BoResultFormat;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.boQuery.BoQueryUtils;
import com.authine.mvp.app.launcher.domain.BOListQry;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.utils.JsonParseObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liwei
 * @DateTime: 2021/7/19
 * @Description: TODO
 */
@Slf4j
public class VoucherRepository {

    private NamedParameterJdbcTemplate jdbcTemplate =  ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);


    /**
     *  新建 凭证数据
     * @param data
     * @return
     */
    public int create(Map<String, Object> data) {
        BoResultFormat.initSys(RequestContext.getCorpLoginId(), data);
        return JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_VOUCHER).putFieldAndVal(data).insert();
    }

    /**
     * 根据 id 删除凭证
     * @param id
     * @return
     */
    public int del(String id) {
        return JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_VOUCHER)
                .putFieldAndVal(KingDeeConstant.DELETED_FIELD, 1)
                .eq(KingDeeConstant.ID_FIELD,id)
                .update();
    }


    public String getTableName() {

        return JdbcTemplateUtils.getTableName(ModelConst.T_VOUCHER,AppConst.FINANCIALCENTER);

    }

    public int deleteByIds(List<String> idList) {

        return JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_VOUCHER)
                .putFieldAndVal(KingDeeConstant.DELETED_FIELD, 1)
                .in(KingDeeConstant.ID_FIELD,idList)
                .update();
    }

    public List<Voucher> queryByIds(List<String> idList) {

        if (CollectionUtils.isEmpty(idList)) {
            return null;
        }

        //TODO 为啥是这样的查询逻辑？
        String sql = " SELECT ad_status,ad_coding,subject_coding,remark,debit_amount,credit_amount " +
                " FROM ( SELECT ad_status,ad_coding,subject_coding,remark,SUM( debit_amount ) AS debit_amount,SUM( credit_amount ) AS credit_amount,substring( remark, 0, 5 ) remark1, " +
                " ( CASE WHEN subject_coding = '1405.01' THEN 1 WHEN subject_coding = '2202.03.02' THEN 1 WHEN subject_coding = '1405.02' THEN 2 WHEN subject_coding = '2202.05.02' THEN 2 ELSE 0 END ) note  " +
                " FROM "+getTableName()+" WHERE id IN ( :ids ) GROUP BY ad_status,ad_coding,subject_coding,remark ) a " +
                " ORDER BY remark1,note,abs( debit_amount ) DESC ";


        Map<String, Object> map = new HashMap<>();

        map.put("ids",idList);
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql, map);

        log.info("凭证记录查询 result:{}",mapList);

        if(CollectionUtils.isEmpty(mapList)){
            return null;
        }
        List<Voucher> voucherList = JsonParseObject.parseObject(JSON.toJSONString(mapList), Voucher.class);
        return voucherList;
    }

    public List<Voucher> queryByDateStr(String time) {

        if (StringUtils.isBlank(time)) {
            return null;
        }
        //SELECT * FROM i_financialcenter_t_voucher1 where DATE_FORMAT(createdTime, '%Y-%m-%d') = "2021-10-23"
        List<Map<String, Object>> mapList = JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_VOUCHER).selectField(" * ").eq(" DATE_FORMAT(createdTime, '%Y-%m-%d') ", time).queryForList();
        log.info("=================根据时间获取对应的凭证数据 queryByDateStr >>>>>  mapList:{}", mapList);
        if(CollectionUtils.isEmpty(mapList)){
            return null;
        }
        List<Voucher> voucherList = JsonParseObject.parseObject(JSON.toJSONString(mapList), Voucher.class);
        return voucherList;
    }
}
